﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRVKOTD(DataSet db)
        {
            int x = 4, y = 1;
            string VKPPOSTYP = "";
            DataTable dtOutput = new DataTable();
            try
            {
                /*
            <DataGridTextColumn Binding="{Binding Path=VKPLIEFTERM}" Header="交期"  Width="Auto" MinWidth="40"/>
            <DataGridTextColumn Binding="{Binding Path=VKPERLDAT}" Header="实际完成"  Width="Auto" MinWidth="40"/>
            <DataGridTextColumn Binding="{Binding Path=DELAYDAYS}" Header="拖期天数"  Width="Auto" MinWidth="40"/>*/
                #region======Title===========
                exAgent.SetCellValue(3, 1, "类型"); dtOutput.Columns.Add("VKPPOSTYP", typeof(System.String));
                exAgent.SetCellValue(3, 2, " "); dtOutput.Columns.Add("VKPAUFKRS", typeof(System.String));
                exAgent.SetCellValue(3, 3, "PPO"); dtOutput.Columns.Add("VKPAUFLFD", typeof(System.String));
                exAgent.SetCellValue(3, 4, "接单日期"); dtOutput.Columns.Add("VKPNEUDAT", typeof(System.String));
                exAgent.SetCellValue(3, 5, "申购单号"); dtOutput.Columns.Add("VKPKDKOMM", typeof(System.String));
                exAgent.SetCellValue(3, 6, "客户"); dtOutput.Columns.Add("KDKUNDNA", typeof(System.String));
                exAgent.SetCellValue(3, 7, "品种"); dtOutput.Columns.Add("VKPART", typeof(System.String));
                exAgent.SetCellValue(3, 8, "色号"); dtOutput.Columns.Add("VKPFARBE", typeof(System.String));
                exAgent.SetCellValue(3, 9, "成份"); dtOutput.Columns.Add("VKCOM", typeof(System.String));
                exAgent.SetCellValue(3, 10, "客人色号"); dtOutput.Columns.Add("VKPKDFARBNR", typeof(System.String));
                exAgent.SetCellValue(3, 11, "合同号"); dtOutput.Columns.Add("VKKBESTNR", typeof(System.String));
                exAgent.SetCellValue(3, 12, "强力标准"); dtOutput.Columns.Add("STRENGTH", typeof(System.String));
                exAgent.SetCellValue(3, 13, "捻度标准"); dtOutput.Columns.Add("COLORSTD", typeof(System.String));
                exAgent.SetCellValue(3, 14, "颜色标准"); dtOutput.Columns.Add("TWIST", typeof(System.String));
                exAgent.SetCellValue(3, 15, "用途"); dtOutput.Columns.Add("VAAPRGR", typeof(System.String));
                exAgent.SetCellValue(3, 16, "数量"); dtOutput.Columns.Add("VKPMENGE1", typeof(System.Decimal));
                exAgent.SetCellValue(3, 17, "成纱量"); dtOutput.Columns.Add("VKPZUGET", typeof(System.Decimal));
                exAgent.SetCellValue(3, 18, "已出货"); dtOutput.Columns.Add("VKPFREIGE", typeof(System.Decimal));
                exAgent.SetCellValue(3, 19, "库存量"); dtOutput.Columns.Add("STOCKQTY", typeof(System.Decimal));
                exAgent.SetCellValue(3, 20, "单位"); dtOutput.Columns.Add("VKPMEEH", typeof(System.String));
                exAgent.SetCellValue(3, 21, "溢短装"); dtOutput.Columns.Add("OVERSHORT", typeof(System.String));
                exAgent.SetCellValue(3, 22, "价格"); dtOutput.Columns.Add("VKPPREIS", typeof(System.Decimal));
                exAgent.SetCellValue(3, 23, "PUM"); dtOutput.Columns.Add("PUM", typeof(System.String));
                exAgent.SetCellValue(3, 24, "交期"); dtOutput.Columns.Add("VKPLIEFTERM", typeof(System.String));
                exAgent.SetCellValue(3, 25, "实际完成"); dtOutput.Columns.Add("VKPERLDAT", typeof(System.String));
                exAgent.SetCellValue(3, 26, "拖期天数"); dtOutput.Columns.Add("DELAYDAYS", typeof(System.Decimal));
                exAgent.SetCellValue(3, 27, "是否拖期"); dtOutput.Columns.Add("ISDELAY", typeof(System.String));
                exAgent.SetCellValue(3, 28, "欠数"); dtOutput.Columns.Add("QTYDIFF", typeof(System.Decimal));
                exAgent.SetCellValue(3, 29, "欠否"); dtOutput.Columns.Add("ISDIFF", typeof(System.String));
                #endregion

                #region======ConvertData===
                dtOutput.Merge(db.Tables["VKPOS"], false, MissingSchemaAction.Ignore);
                int ttn = 0;
                int nDelay = 0;
                decimal ttq = 0;
                DataRow dwVKP = dtOutput.Rows[0];
                string xmonth = dwVKP["VKPERLDAT"].ToString().Substring(0, 6);
                exAgent.RenameSheet("OTD_" + xmonth);
                exAgent.SetCellValue(2, 2, xmonth);
                var query = from XD in dtOutput.AsEnumerable() 
                            group XD by new
                            {
                                XM = XD["VKPERLDAT"].ToString().Substring(0,6),
                                ISDELAY = XD["ISDELAY"].ToString(), 
                            }
                                into g
                                select new
                                {
                                    KEY = g.Key,
                                    nDelay=g.Count(),
                                    VKPMENGE1 = g.Sum(XD => XD.Field<Decimal>("VKPMENGE1")), 
                                };
                foreach (var GW in query)
                {
                    ttq+= (decimal)GW.VKPMENGE1;
                    ttn += (int)GW.nDelay;
                    if (((string)GW.KEY.ISDELAY)!= "Y")
                    {
                        nDelay= (int)GW.nDelay;
                        exAgent.SetCellValue(2, 10, nDelay);
                    } 
                }
                exAgent.SetCellValue(1, 6,Math.Round(ttq/1000)+"吨");
                exAgent.SetCellValue(1, 10, ttn);
                exAgent.SetCellValue(2, 3,Math.Round((decimal)100*nDelay/ttn,1));
                exAgent.SetCellValue(2, 6, Math.Round((decimal)ttq / ttn, 1)+"KG");    
                #endregion
                #region====Excel==========                
                int count = dtOutput.Rows.Count;
                exAgent.RangeCopy(4, 29, 5, 4 + count);
                exAgent.DataTableToExcel(dtOutput, x, 1);
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(5, 16, count + 3, 19);
                #endregion

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
        }

    }
}